package com.sky.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Resource
    private DishMapper dishMapper;

    @Resource
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public Integer addCart(ShoppingCartDTO shoppingCartDTO) {
        Integer tage = 0;
        //1.查询历史购物车,操作数据库,我们都使用实体类
        // 查询之前，需要把用户ID，放到shoppingCartDTO里面(需要自己在DTO增加userId)
        shoppingCartDTO.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByList(shoppingCartDTO);
        // 2。 如果基于当前用户Id\当前商品Id\当前口味 没有数据，插入
        if (shoppingCartList == null || shoppingCartList.size() == 0) {
            ShoppingCart shoppingCart = new ShoppingCart();
            // 先设置能设置字段
            shoppingCart.setUserId(shoppingCartDTO.getUserId());
            shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 再根据当前是否是菜品还是套餐，去查询对应的表，设置冗余字段
            if (shoppingCartDTO.getDishId() != null) {
                shoppingCart.setDishId(shoppingCartDTO.getDishId());
                //根据菜品Id ，去查询菜品信息
                Dish dish = dishMapper.findById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());//菜品或套餐
                shoppingCart.setAmount(dish.getPrice());//价格
                shoppingCart.setImage(dish.getImage());//图片
            } else {
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                // 根据套餐Id ，去查询套餐的信息
                SetmealVO setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());//菜品或套餐
                //shoppingCart.setSetmealId();
                shoppingCart.setAmount(setmeal.getPrice());//价格
                shoppingCart.setImage(setmeal.getImage());//图片
            }
            //添加数据库
            log.info("[添加购物车之前] shoppingCart:{}", shoppingCart);
            tage = shoppingCartMapper.insert(shoppingCart);
        } else {
            // 有数据，更新份数
            ShoppingCart shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            tage = shoppingCartMapper.update(shoppingCart);
        }
        return tage;

    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> findAll() {
        Long currentId = BaseContext.getCurrentId();

        return shoppingCartMapper.selectByUserId(currentId);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @Override
    public Integer clean() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.deleteByUserId(userId);
    }
}
